<?php
namespace ORG;
class Sms{
	private static $url ='http://api.sms.cn/sms/';
	private static $codeArr = [
		'100' => '发送成功',
		'101' => '验证失败',
		'102' => '短信不足',
		'103' => '操作失败',
		'104' => '非法字符',
		'105' => '内容过多',
		'106' => '号码过多',
		'107' => '频率过快',
		'108' => '号码内容空',
		'109' => '账号冻结',
		'112' => '号码错误',
		'113' => '定时出错',
		'116' => '禁止接口发送',
		'117' => '绑定IP不正确',
		'161' => '未添加短信模板',
		'162' => '模板格式不正确',
		'163' => '模板ID不正确',
		'164' => '全文模板不匹配',
	];

	static function get($data){
		$url=self::$url.'?'.http_build_query($data);
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
	    /*curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
	    curl_setopt($ch, CURLOPT_REFERER,_REFERER_);*/
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	    $r = curl_exec($ch);
	    curl_close($ch);

        if(\Zend\Stdlib\StringUtils::isValidUtf8($r)){
        }else{
        	$r = iconv('GB2312','UTF-8',$r);
        }
	    return json_decode($r,true);
	}
    static function send($phone=null,$content=[]){
    	$data = [
			"ac"=>"send",
			"uid"=>'szszphjryxgs',
			'pwd'=>md5('n1e5a6s6m7szszphjryxgs'),
			'mobile'=>$phone,
		];
		if(array_key_exists('tpl',$content)){
			$data['template'] = $content['tpl'];
			unset($content['tpl']);
			$data['content'] = json_encode($content);
		}else{
			$data['content'] = $content['body'];
		}

		$ret = self::get($data);
		if($ret['stat']==100){
			return true;
		}else{
			return $ret['message'];			
		}
    }
    static function status($phone=null,$content=[]){
		$ret = post('http://api.sms.cn/sms/',[
			"ac"=>"status",
			"uid"=>'szszphjryxgs',
			'pwd'=>md5('n1e5a6s6m7szszphjryxgs'),
		]);
		/*
		{
			"stat":"100",
			"total":"50",
			"values":[
				{"retime":"2010-11-14 18:04:19","mobile":"13900008888","mobileids":"1390000888800001","resultcode":"100","status":"DELIVRD"},
				{"retime":"2010-11-14 18:09:19","mobile":"13900008888","mobileids":"1390000888800001","resultcode":"100","status":"DELIVRD"}
			]
		}
		*/
    }
    static function reply($phone=null,$content=[]){
		$ret = post('http://api.sms.cn/sms/',[
			"ac"		=>"reply",
			'format'	=>'json',
			"uid"=>'szszphjryxgs',
			'pwd'=>md5('n1e5a6s6m7szszphjryxgs'),
		]);
		/*
		{
			"stat":"100",
			"total":"100",
			"values":[
				{"retime":"2012-04-13 10:00:23","mobile":"15900001111","content":"谢谢你！"},
				{"retime":"2012-04-13 09:59:50","mobile":"15900001111","content":"收到"}
			]}
		*/
    }
}